#include "unit_test/suite.h"
#include "RGB_LED_USART.h"
#include "interrupt.h"

#include "TWI_Master.h"
#include "twi_func.h"
#include <stdio.h>
#include <stdint.h>
#include <ctype.h>
#include <avr/io.h>

int printCHAR(char character, FILE *stream) {
	USART_PutChar(character);
	return 0;
}

FILE uart_str = FDEV_SETUP_STREAM(printCHAR, NULL, _FDEV_SETUP_RW);


int plus(int a, int b);

void run_1(const struct test_case *test) {
	test_assert_true(test,plus(5,6)==11,"Test result = 11");
}

void twi_setup() {
	TWI_Master_Initialise();
}

#define PCA_ADDR 0b1110000

void twi_test() {
	unsigned char twi_buf[4];
	
	printf("Test TWI\r\n");
	twi_buf[0]=PCA_ADDR<<1;
	twi_buf[1]=0b11011;
	TWI_Start_Transceiver_With_Data(twi_buf,2);

	twi_buf[0]=(PCA_ADDR<<1)|1;
	TWI_Start_Transceiver_With_Data(twi_buf,1);
	
	while(TWI_Transceiver_Busy()) {
	}
	
	if(TWI_statusReg.lastTransOK) {
		TWI_Get_Data_From_Transceiver(twi_buf,2);
		printf("\r\nresult from PCA:%x",twi_buf[1]);
		} else {
		printf("\r\n Error from PCA:%x",TWI_Get_State_Info());
	}
	
}


int main(void) {
	int i;
	
	USART_Init();
	twi_setup();

	sei();
	stdout = &uart_str;
	
	USART_SendStr("Start TWI test\r\n");
	twi_test();
	USART_SendStr("Test SendSTR\r\n");
	

	printf("Hello World 2\n\r");
	
	for(i=0;i<20;i++){
		USART_PutChar(i+48);
	}
	
	USART_SendStr("\r\nend\r\n\r\n");

	DEFINE_TEST_CASE(test_1,
	NULL,
	run_1,
	NULL,
	"First test");
	
	DEFINE_TEST_ARRAY(my_tests) = {
		&test_1,
	};

	DEFINE_TEST_SUITE(my_suite,my_tests,"One-test suite");

	test_suite_run(&my_suite);
	
	while(1);
	
}